//
//  SEOnlineReserveTextCell.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/9.
//

import UIKit

class SEOnlineReserveTextCell: UITableViewCell {
    
    private var viewModel: SEInquiryInfoViewModel?
    private var endEditing: ((SEInquiryInfoViewModel) -> Void)?
    private var currentIndexPath: IndexPath?

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .white
        contentView.backgroundColor = .white
    }
    
    func update(with viewModel: SEInquiryInfoViewModel, indexPath: IndexPath, endEditing: ((SEInquiryInfoViewModel) -> Void)?) {
        self.viewModel = viewModel
        self.endEditing = endEditing
        self.currentIndexPath = indexPath
        textView.isUserInteractionEnabled = viewModel.patientDidPicked
        if indexPath.section == 2 {
            titleLabel.text = "病情描述"
            textView.placeholder = "请描述您的症状以便于医生判断病情。为保障隐私安全，请勿透露您的姓名和联系方式"
            textView.text = viewModel.info.illness
        } else if indexPath.section == 4 {
            titleLabel.text = "既往史"
            textView.placeholder = "请描述您的既往史（既往的健康状况和过去曾经患过的疾病等方面的问题，既往健康状况包括饮食习惯等。）"
            textView.text = viewModel.info.pastHistory
        }
    }
    
    @IBOutlet private weak var titleLabel: UILabel!
    
    @IBOutlet private weak var textView: SETextView! {
        didSet {
            textView.tvDelegate = self
        }
    }
}

// MARK: - SETextViewDelegate

extension SEOnlineReserveTextCell: SETextViewDelegate {
    func textViewDidEndEditing(_ textView: UITextView) {
        guard let viewModel = viewModel, let indexPath = currentIndexPath else { return }
        if indexPath.section == 2 {
            viewModel.info.illness = textView.text
        } else if indexPath.section == 4 {
            viewModel.info.pastHistory = textView.text
        }
        endEditing?(viewModel)
    }
}
